package com.shop.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shop.pojo.Employee;
import com.shop.service.EmployeeService;
import com.shop.utils.Constants;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService; 


	@RequestMapping("/login")
	public String login(String username,String password,HttpSession session,Model model) {
		Employee employee = employeeService.isLogin(username);
		if (employee != null) {
			if (employee.getPassword().equals(password)) {
				session.setAttribute(Constants.GLOBLE_USER_SESSION, employee);
				model.addAttribute("username",username);
				return "index";
			}else {
				model.addAttribute("errorMsg", "账号或密码存在错误");
				return "login";
			}
		}else {
			model.addAttribute("errorMsg", "账号或密码存在错误");
			return "login";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/login.jsp";
	}
	
}
